package com.ets.Address_DAO;

import com.ets.common.DataDictionaryConstants;
import com.ets.common.Displayable;
import com.ets.common.StringUtils;
import com.ets.common.Validator;

/*------------------------------------------------------------------------------
 The <big><b>Address</b></big> class implements the <b>Displayable</b> &
 <b>Cloneable</b> interfaces to create an <b>Address</b> object
 <br><br>
 @author D.Merkling - 1:33:38 PM - Aug 22, 2011
 <br><br>
 @version 1.2  
------------------------------------------------------------------------------*/
public class Address implements Displayable, Cloneable, DataDictionaryConstants
  {
   private int    Addr;
   private String Strt;
   private String Strt2;
   private String City;
   private String State;
   private String Zip;
   
   /*------------------------------------------------------------------------------
	This constructor creates a new <b>Address</b> object with default values
   ------------------------------------------------------------------------------*/
   public Address ()
	 {
	  this.Addr  = 0;
	  this.Strt  = " ";
	  this.Strt2 = " ";
	  this.City  = " ";
	  this.State = " ";
	  this.Zip   = " ";
	 }

   /*------------------------------------------------------------------------------
	This constructor creates a new <b>Address</b> object with attribute values as
	defined by the provided parameters
	<br><br>
	@param Addr An <b><i>int</i></b> variable
	@param Street address #1 A <b><i>String</i></b> variable
	@param Street address #2 A <b><i>String</i></b> variable
	@param City code A <b><i>String</i></b> variable
	@param State code A <b><i>String</i></b> variable
	@param Zip Code A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public Address (int Addr, String Strt, String Strt2, String City, String State, String Zip)
	 {
	  this.Addr  = Addr;
	  this.Strt  = Strt;
	  this.Strt2 = Strt2;
	  this.City  = City;
	  this.State = State;
	  this.Zip   = Zip;
	 }
   
   /*------------------------------------------------------------------------------
	Clones the current <b>Address</b> object to create a new one
	<br><br>
	@return Address - A <b>Address</b> object
	@throws CloneNotSupportedException 
   ------------------------------------------------------------------------------*/
   public Object clone () throws CloneNotSupportedException
	 {
	  return super.clone();
	 }

   /*------------------------------------------------------------------------------
	Sets the <b><i>int</i></b> <b>Addr</b> attribute for the current
	<b>Address</b> object
	<br><br>
	The value must be a positive integer
	<br><br>
	@param Addr An <b><i>int</i></b> variable
   ------------------------------------------------------------------------------*/
   public void setAddr (int addr)
	 {
	  this.Addr = addr;
	 }

   /*------------------------------------------------------------------------------
	Retrieves the <b><i>int</i></b> <b>Addr</b> attribute from the current
	<b>Address</b> object
	<br><br>
	@return Addr - An <b><i>int</i></b> variable
   ------------------------------------------------------------------------------*/
   public int getAddr ()
	 {
	  return Addr;
	 }

   /*------------------------------------------------------------------------------
	Sets the <b><i>String</i></b> <b>Strt</b> attribute for the current
	<b>Address</b> object
	<br><br>
	The value must be trimmed & left-justified
	<br><br>
	@param Street address #1 A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public void setStrt (String strt)
	 {
	  String tmpStrt = strt.trim();
	  this.Strt      = tmpStrt;
	 }

   /*------------------------------------------------------------------------------
	Retrieves the <b><i>String</i></b> <b>Strt</b> attribute from the current
	<b>Address</b> object
	<br><br>
	@return Street address #1 - A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public String getStrt ()
	 {
	  return Strt;
	 }

   /*------------------------------------------------------------------------------
	Sets the <b><i>String</i></b> <b>Strt2</b> attribute for the current
	<b>Address</b> object
	<br><br>
	The value must be trimmed & left-justified
	<br><br>
	@param Street address #2 A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public void setStrt2 (String strt)
	 {
	  String tmpStrt = strt.trim();
	  this.Strt2     = tmpStrt;
	 }

   /*------------------------------------------------------------------------------
	Retrieves the <b><i>String</i></b> <b>Strt2</b> attribute from the current
	<b>Address</b> object
	<br><br>
	@return Street address #2 - A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public String getStrt2 ()
	 {
	  return Strt2;
	 }

   /*------------------------------------------------------------------------------
	Sets the <b><i>String</i></b> <b>City</b> code attribute for the current
	<b>Address</b> object
	<br><br>
	The value must be uppercase, trimmed, left-justified, & exist in the cd_val
	table in the ETS/CTY segment
	<br><br>
	@param City code A <b><i>String</i></b> variable
	@return Success/failure - A <b><i>boolean</i></b> variable
   ------------------------------------------------------------------------------*/
   public boolean setCity (String cty)
	 {
      String tmpCty   = cty.trim();
	         tmpCty   = tmpCty.toUpperCase();
      boolean isValid = Validator.isCodeValid("ETS", "CTY", tmpCty);
      if (isValid)
	    {
	     this.City    = tmpCty;
	     return true;
	    }
	  else
	    {
	     return false;
	    }
	 }

   /*------------------------------------------------------------------------------
	Retrieves the <b><i>String</i></b> <b>City</b> attribute from the current
	<b>Address</b> object
	<br><br>
	@return City code - A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public String getCity ()
	 {
	  return City;
	 }

   /*------------------------------------------------------------------------------
	Sets the <b><i>String</i></b> <b>State</b> code attribute for the current
	<b>Address</b> object
	<br><br>
	The value must be uppercase, trimmed, left-justified, & exist in the cd_val
	table in the ETS/ST segment
	<br><br>
	@param State code A <b><i>String</i></b> variable
	@return Success/failure - A <b><i>boolean</i></b> variable
   ------------------------------------------------------------------------------*/
   public boolean setState (String st)
	 {
      String tmpSt    = st.trim();
	         tmpSt    = tmpSt.toUpperCase();
      boolean isValid = Validator.isCodeValid("ETS", "ST", tmpSt);
      if (isValid)
	    {
	     this.State   = tmpSt;
	     return true;
	    }
	  else
	    {
	     return false;
	    }
	 }

   /*------------------------------------------------------------------------------
	Retrieves the <b><i>String</i></b> <b>State</b> code attribute from the current
	<b>Address</b> object
	<br><br>
	@return State code - A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public String getState ()
	 {
	  return State;
	 }

   /*------------------------------------------------------------------------------
	Sets the <b><i>String</i></b> <b>Zip</b> code attribute for the current
	<b>Address</b> object
	<br><br>
	The value must be uppercase, trimmed, left-justified, & formatted as a proper
	Zip Code or Zip Code+4
	<br><br>
	@param Zip code A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public boolean setZip (String inZp)
	 {
      String zp1 = inZp.trim();
	         zp1 = zp1.toUpperCase();
	  String zp2 = "";
	         zp2 = Validator.validateZip(zp1);
	  if (zp2.equals("**BAD**"))
		{
	     System.out.println("Zip Code not valid; saved as blanks");
		 return false;
		}
	  else
		{
		 this.Zip = zp2;
		 return true;
		}
	 }

   /*------------------------------------------------------------------------------
	Retrieves the <b><i>String</i></b> <b>Zip</b> code attribute from the current
	<b>Address</b> object
	<br><br>
	@return Zip code - A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public String getZip ()
	 {
	  return Zip;
	 }
   
   /*------------------------------------------------------------------------------
	Determines whether or not all of the attribute values of one <b>Address</b>
	object are equal to all of the attribute values of a second <b>Address</b>
	object
	<br><br>
	@param object A	<b>Address</b> object
	@return true/false - A <b><i>boolean</i></b> variable
   ------------------------------------------------------------------------------*/
   public boolean equals(Object object)
     {
      if (object instanceof Address)
        {
         Address addr2 = (Address) object;
         if (Addr == addr2.getAddr()        &&
             Strt.equals(addr2.getStrt())   &&
             Strt2.equals(addr2.getStrt2()) &&
             City.equals(addr2.getCity())   &&
             State.equals(addr2.getState()) &&
             Zip.equals(addr2.getZip()))
         return true;
        }
      return false;
     }

   /*------------------------------------------------------------------------------
	Retrieves a one-line formatted list of the attribute values of the current
	<b>Address</b> object for use in a table display
	<br><br>
	@return string - A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public String toTableString ()
	 {
	  return this.Addr                                        + " | " +
	         StringUtils.padWithSpaces(this.Strt,  Strt_LEN)  + " | " +
	         StringUtils.padWithSpaces(this.Strt2, Strt_LEN)  + " | " +
	         StringUtils.padWithSpaces(this.City,  City_LEN)  + " | " +
	         StringUtils.padWithSpaces(this.State, State_LEN) + " | " +
	         StringUtils.padWithSpaces(this.Zip,   Zip_LEN);
	 }
   
   /*------------------------------------------------------------------------------
	Retrieves a formatted list of the attribute values of the current
	<b>Address</b> object
	<br><br>
	@return string - A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public String toString()
	 {
	  return "Address  : " + this.Addr  + "\n" +
	         "Street #1: " + this.Strt  + "\n" +
		     "Street #2: " + this.Strt2 + "\n" +
		     "City     : " + this.City  + "\n" +
		     "State    : " + this.State + "\n" +
		     "Zip Code : " + this.Zip   + "\n";
	 }
   
   /*------------------------------------------------------------------------------
	Retrieves a formatted displayable list of the attribute values of the current
	<b>Address</b> object
	<br><br>
	@return string - A <b><i>String</i></b> variable
   ------------------------------------------------------------------------------*/
   public String getDisplayText()
    {
	  return toString();
    }
  }
